package com.wangp.mywrite.inwork.sync;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/***
 * @author wangpeng
 * @since 2022-08-31  14:15
 */
public class CallableDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), r -> {
            Thread t = new Thread(r);
            t.setName("myTest");
            return t;
        });
        Future<Integer> future = executor.submit(new TaxCalculator(100));
        while (!future.isDone()) {
            TimeUnit.MILLISECONDS.sleep(200);
            System.out.print("#");
        }
        System.out.println("\ncalculation over,tax is " + future.get() + " YUAN");
        executor.shutdown();
    }
}

class TaxCalculator implements Callable<Integer> {

    private final int seedMoney;

    public TaxCalculator(int seedMoney) {
        this.seedMoney = seedMoney;
    }

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        TimeUnit.MILLISECONDS.sleep(10000);
        return seedMoney / 10;
    }
}
